package com.xuecheng.learning.service.impl;

import com.alibaba.fastjson.JSON;
import com.xuecheng.base.RestResponse;
import com.xuecheng.content.model.po.CoursePublish;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.learning.feignclient.ContentServiceClient;
import com.xuecheng.learning.feignclient.MediaServiceClient;
import com.xuecheng.learning.model.dto.XcCourseTablesDto;
import com.xuecheng.learning.service.LearningService;
import com.xuecheng.learning.service.MyCourseTablesService;
import com.xuecheng.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author 杨振华
 * @since 2023/11/12
 */
@Service
@Slf4j
public class LearningServiceImpl implements LearningService {

    @Autowired
    MyCourseTablesService myCourseTablesService;

    @Autowired
    ContentServiceClient contentServiceClient;

    @Autowired
    MediaServiceClient mediaServiceClient;

    @Override
    public RestResponse<String> getVideo(String userId, Long courseId, Long teachplanId, String mediaId) {
        // 查询课程发布信息
        CoursePublish coursepublish = contentServiceClient.getCoursepublish(courseId);
        if (coursepublish == null) {
            return RestResponse.validfail("课程未发布或不存在！");
        }

        // 根据课程计划id，查看是否支持试学（is_preview）--远程调用课程计划
        Teachplan teachplan = contentServiceClient.getTeachplan(teachplanId);
        if ("1".equals(teachplan.getIsPreview())) {
            return mediaServiceClient.getPlayUrlByMediaId(mediaId);
        }
        // 也可以不调用，解析coursepublish的课程计划的json拿到
        /*String teachplan_json = coursepublish.getTeachplan();
        Map mapType = JSON.parseObject(teachplan_json, Map.class);
        String isPreview = (String) mapType.get("isPreview");
        log.debug("isPreview------>-------->{}", isPreview);
        if (isPreview.equals("1")) {
            return mediaServiceClient.getPlayUrlByMediaId(mediaId);
        }*/

        // 获取学习资格
        if (StringUtils.isNotEmpty(userId)) {
            // 通过我的课程表即可知道用户的资格
            XcCourseTablesDto learningStatus = myCourseTablesService.getLearningStatus(userId, courseId);
            // [{"code":"702001","desc":"正常学习"},{"code":"702002","desc":"没有选课或选课后没有支付"},{"code":"702003","desc":"已过期需要申请续期或重新支付"}]
            if (learningStatus.getLearnStatus().equals("702002")) {
                return RestResponse.validfail("无法学习，原因是没有选课或未进行支付！");
            }else if (learningStatus.getLearnStatus().equals("702003")) {
                return RestResponse.validfail("选课过期，请重新选择！");
            }else {
                // 有学习资格，返回视频播放地址
                // 远程调用媒资服务获取视频
                return  mediaServiceClient.getPlayUrlByMediaId(mediaId);
            }
        }else {
            // 用户未登录
            // 返回课程发布预览信息，取出收费规则
            if ("201000".equals(coursepublish.getCharge())) {
                // 有资格学习
                // 远程调用媒资服务
                return  mediaServiceClient.getPlayUrlByMediaId(mediaId);
            }
        }
        return RestResponse.validfail("该课程没有选课，需要购买！");
    }
}
